<?php
/**
* @author: trungdt
* @version: 1.0
* @since: Jan 26, 2011
* 
* Change logs
* Jan 24, 2011 - trungdt - first create
* jan 25, 2011 - trungdt - Add pastor
* Jan 26, 2011 - trungdt - Add ring
*/
class prayonline_subject {
    /**
    * Build up form object for subjects
    * 
    * @return form
    */
	function subject_form($object, $config=null) {
		if (!defined('SYS_FORMS')) require_once(BASE.'subsystems/forms.php');
		exponent_forms_initialize();
		
		global $db;
		
		$form = new form();
			
		
		if (!isset($object->id)) {
			$object->name = '';
            $object->desc='';
            $object->add_date=0;
		} else {
			$form->meta('id',$object->id);
		}
        
        //
        if (isset($object->id))
        {
            $pastor_subject=$db->selectObjects("pastor_subject","subject_id = {$object->id}");
        }
        else
        {
            $pastor_subject=array();
        }
        $pastors=$db->selectObjects("pastors");
        foreach ($pastors as $key=>$value)
        {
            foreach ($pastor_subject as $k=>$v)
            {
                if ($v->pastor_id == $value->id)
                {
                    $value->checked=true;
                }
            }
            $pastors[$key]=$value;
        }    
        //
        //
        
		$form->validationScript="";
		$form->register('name','Name',new textcontrol($object->name,50,false,200));
        $form->register('desc','Info',new htmleditorcontrol($object->desc));
        $form->register('pastors','Select pastors',new listboxcontrol($pastors, true));
        if ($object->add_date > 0)
        {
           $form->register("add_date","",new htmlcontrol("<input type=hidden name='add_date' value={$object->add_date}>"));
        }
		$form->register('submit','',new buttongroupcontrol('Save','','Cancel'));
		return $form;
	}
	
	/**
    * This function update all info in subject object
    * @author: trungdt
    */
    function subject_update($values,$object) {
		$object->name = $values['name'];
		$object->desc = $values['desc'];
		if (isset($values['add_date']))
        {
            $object->add_date=intval($values['add_date']);
        }
        else
        {
            $object->add_date=time(); // now
        }
		
		return $object;
	}
    
    /**
    * Build up form object for Pastor form
    */
    function pastor_form($object,  $config=null) {
        if (!defined('SYS_FORMS')) require_once(BASE.'subsystems/forms.php');
        exponent_forms_initialize();
        
        global $db;
        
        $form = new form();
            
        
        if (!isset($object->id)) {
            $object->name = '';
            $object->desc='';
            $object->user_id=-1;
        } else {
            $form->meta('id',$object->id);
        }
        $form->validationScript="";
        
        if (isset($object->id))
        {
            $pastor_subject=$db->selectObjects("pastor_subject","pastor_id = {$object->id}");
        }
        else
        {
            $pastor_subject=array();
        }
        $subjects=$db->selectObjects("subjects");
        foreach ($subjects as $key=>$value)
        {
            foreach ($pastor_subject as $k=>$v)
            {
                if ($v->subject_id == $value->id)
                {
                    $value->checked=true;
                }
            }
            $subjects[$key]=$value;
        }    
        //
               
        $form->register('name','Name',new textcontrol($object->name,50,false,200));
        $form->register('desc','Info',new htmleditorcontrol($object->desc));
        $form->register('user_id','Alias to user account',new userscontrol($object->user_id));
        $form->register('subjects','Select subjects',new listboxcontrol($subjects, true));
        $form->register('submit','',new buttongroupcontrol('Save','','Cancel'));
        return $form;
    }
     
     /**
     * This form save all info for pastor object
     * 
     */
     function pastor_update($values,$object) {
        $object->name = $values['name'];
        $object->desc = $values['desc'];
        $object->user_id = $values['user_id'];
        
        return $object;
    }
    
    
    /**
    * Build up form object for Ring form
    * @return form
    */
    function ring_form($object,  $config=null) {
        if (!defined('SYS_FORMS')) require_once(BASE.'subsystems/forms.php');
        exponent_forms_initialize();
        
        global $db;
        
        $form = new form();
            
        
        if (!isset($object->id)) {
            $object->name = '';
            $object->desc = '';
        } else {
            $form->meta('id',$object->id);
        }
        $form->validationScript="";
        
        //
        //
        if (isset($object->id))
        {
            $pastor_ring=$db->selectObjects("pastor_ring","ring_id = {$object->id}");
        }
        else
        {
            $pastor_ring=array();
        }
        $pastors=$db->selectObjects("pastors");
        foreach ($pastors as $key=>$value)
        {
            foreach ($pastor_ring as $k=>$v)
            {
                if ($v->pastor_id == $value->id)
                {
                    $value->checked=true;
                }
            }
            $pastors[$key]=$value;
        }    
        //
        //
               
        $form->register('name','Name',new textcontrol($object->name,50,false,200));
        $form->register('desc','Info',new htmleditorcontrol($object->desc));
        $form->register('pastors','Select pastors',new listboxcontrol($pastors, true));
        // default ring
        $temp=new checkboxcontrol($object->default_ring);
        $temp->flip=true;
        $form->register('default_ring','&nbsp;Đây là vòng cầu nguyện mặc định cho tất cả các nhu cầu',$temp);
        $form->register('submit','',new buttongroupcontrol('Save','','Cancel'));
        return $form;
    }
     
     /**
     * This form save all info for pastor object
     * 
     */
     function ring_update($values,$object) {
        $object->name = $values['name'];
        $object->desc = $values['desc'];
        if (isset($values['default_ring']))
            $object->default_ring = 1;
        else
            $object->default_ring = 0;
        return $object;
    }
}

?>
